package com.pactera.madp.cp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.entity.TakeOverEntity;
import com.pactera.madp.cp.mapper.TakeOverMapper;
import com.pactera.madp.cp.service.TakeOverService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
public class TakeOverServiceImpl extends ServiceImpl<TakeOverMapper, TakeOverEntity> implements TakeOverService {

    @Override
    public void add(Integer corpId) {
        LocalDate week = LocalDate.now();
        LocalDate day = LocalDate.now();
        LocalDate month = LocalDate.now();
        int dayOfMonth = month.getDayOfMonth();
        int dayOfWeek = week.getDayOfWeek().getValue();
        week = week.minusDays(dayOfWeek - 1);
        month = month.minusDays(dayOfMonth - 1);
        Boolean existWeek = baseMapper.isExist(corpId, week, 2);
        Boolean existDay = baseMapper.isExist(corpId, day, 1);
        Boolean existMonth = baseMapper.isExist(corpId, month, 3);
        TakeOverEntity entity = new TakeOverEntity();
        if (!existWeek) {
            entity.setCreateDay(week);
            entity.setDailyType(2);
            entity.setCorpId(corpId);
            this.save(entity);
        }
        if (!existDay) {
            entity.setCreateDay(day);
            entity.setDailyType(1);
            entity.setCorpId(corpId);
            this.save(entity);
        }
        if (!existMonth) {
            entity.setCreateDay(month);
            entity.setDailyType(3);
            entity.setCorpId(corpId);
            this.save(entity);
        }

        baseMapper.statisic(corpId, day, 1);
        baseMapper.statisic(corpId, month, 3);
        baseMapper.statisic(corpId, week, 2);
    }

    @Override
    public Integer getTakeOverCount(Integer corpId, LocalDate date, Integer dailyType) {
        Integer count = baseMapper.getTakeOverCount(corpId, date, dailyType);
        return count == null? 0 : count;
    }
}
